package com.xhd.db;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import java.util.Vector;

import com.xhd.base.log.Log;
import com.xhd.base.log.LogFactory;
import com.xhd.exception.DatabaseException;

public class XDBPool
implements DBPool, Serializable
{
private static final long jm51 = 1L;
private DBConfig jm53;
private int jm220 = 0;
private Vector jm221 = new Vector();
private int jm222 = 20;
private final int jm223 = 250;
private final int jm224 = 1;
private final int jm225 = 2;
private final int jm226 = 4;
private final int jm227 = 5;
private final int jm228 = 8;
private final int jm229 = 16;
private static final Log jm9;
private DaoMonitor jm54;
private Driver jm230;
static Class jm10;

static
{
  Class tmp3_0 = jm10;
  if (tmp3_0 == null)
  {
   
    try
    {
        XDBPool.jm10 = Class.forName("com.xhd.systemdbmanager.XDBPool");
    }
    catch (ClassNotFoundException localClassNotFoundException)
    {
      throw new NoClassDefFoundError(localClassNotFoundException.getMessage());
    }
  }
  jm9 = LogFactory.getLog(tmp3_0);
}

protected XDBPool(DBConfig paramDBConfig)
{
  this.jm53 = paramDBConfig;
  this.jm54 = DaoMonitor.getInstance();
}

public int getLoginTimeout()
  throws SQLException
{
  return (this.jm222 * 250 / 1000);
}

public void setLoginTimeout(int paramInt)
  throws SQLException
{
  if (paramInt <= 0)
    return;
  this.jm222 = (paramInt * 1000 / 250);
}

public DBConn getConnection()
  throws DatabaseException
{
  OutParam localOutParam = new OutParam();
  DBConn localDBConn = jm219(1, null, localOutParam);
  if ((localDBConn == null) && (localOutParam.jm18))
  {
    localDBConn = jm218();
    jm219(5, localDBConn, localOutParam);
  }
  if (localDBConn == null)
  {
    for (int i = 0; i < this.jm222; ++i)
    {
      try
      {
        Thread.sleep(250L);
      }
      catch (InterruptedException localInterruptedException)
      {
      }
      localDBConn = jm219(1, null, localOutParam);
      if (localDBConn != null)
        break;
      if (!(localOutParam.jm18))
        continue;
      localDBConn = jm218();
      jm219(5, localDBConn, localOutParam);
      break;
    }
    if (localDBConn == null)
      throw new DatabaseException(30044, this.jm53.getName(), String.valueOf(this.jm220));
  }
  return localDBConn;
}

public DBConn getInnerConnection()
  throws DatabaseException
{
  OutParam localOutParam = new OutParam();
  DBConn localDBConn = jm219(16, null, localOutParam);
  if ((localDBConn == null) && (localOutParam.jm18))
  {
    localDBConn = jm218();
    jm219(5, localDBConn, localOutParam);
  }
  return localDBConn;
}

public void freeConnection(DBConn paramDBConn)
{
  try
  {
    if (paramDBConn.isBad())
    {
      jm9.error("Close bad connection: " + paramDBConn.getId());
      jm219(4, paramDBConn, null);
      return;
    }
    jm219(2, paramDBConn, null);
  }
  catch (Exception localException)
  {
  }
}

protected void release()
{
  for (int i = 0; i < this.jm221.size(); ++i)
  {
    Connection localConnection = (Connection)this.jm221.get(i);
    try
    {
      localConnection.close();
    }
    catch (SQLException localSQLException)
    {
      localSQLException.printStackTrace();
    }
  }
  this.jm221.removeAllElements();
}

private DBConn jm218()
  throws DatabaseException
{
  DBConn localDBConn = null;
  try
  {
    Connection localConnection = null;
    Properties localProperties = new Properties();
    if ((this.jm53.getLanguage() != null) && (this.jm53.getLanguage().length() > 0))
      localProperties.put(DBUtil.getLangProp(getDBType()), this.jm53.getLanguage());
    if (this.jm53.getUser() != null)
    {
      localProperties.put("user", this.jm53.getUser());
      localProperties.put("password", this.jm53.getPassword());
    }
    if (this.jm230 != null)
      localConnection = this.jm230.connect(this.jm53.getURL(), localProperties);
    else
      localConnection = DriverManager.getConnection(this.jm53.getURL(), localProperties);
    if (localConnection != null)
    {
      localDBConn = new DBConn(this, localConnection, this.jm53);
      localDBConn.setCheckConn(this.jm53.isCheckConn());
    }
  }
  catch (Throwable localThrowable)
  {
    jm9.error(this.jm53.getURL());
    jm9.error(localThrowable);
    throw new DatabaseException(30044, this.jm53.getName(), String.valueOf(this.jm220));
  }
  return localDBConn;
}

private synchronized DBConn jm219(int paramInt, DBConn paramDBConn, OutParam paramOutParam)
  throws DatabaseException
{
  if (paramInt == 1)
  {
    paramDBConn = null;
    if (this.jm221.size() > 0)
    {
      paramDBConn = (DBConn)this.jm221.remove(0);
      paramDBConn.reInit();
    }
    else
    {
      paramOutParam.jm18 = (this.jm220 < this.jm53.getMaxConnections());
    }
    if (paramDBConn != null)
    {
      this.jm220 += 1;
      this.jm54.counterAdd();
    }
    return paramDBConn;
  }
  if ((paramInt == 2) && (paramDBConn != null))
  {
    if (this.jm221.size() >= this.jm53.getMaxIdleConn())
      try
      {
        paramDBConn.release();
        paramDBConn = null;
      }
      catch (Throwable localThrowable)
      {
        jm9.error(localThrowable);
      }
    else
      this.jm221.add(paramDBConn);
    if (this.jm220 > 0)
      this.jm220 -= 1;
    this.jm54.counterSub();
  }
  else if ((paramInt == 4) && (paramDBConn != null))
  {
    try
    {
      paramDBConn.release();
    }
    catch (Exception localException1)
    {
      jm9.error(localException1);
    }
    if (this.jm220 > 0)
      this.jm220 -= 1;
    this.jm54.counterSub();
  }
  else if (5 == paramInt)
  {
    if (paramDBConn != null)
    {
      this.jm220 += 1;
      this.jm54.counterAdd();
    }
  }
  else if (paramInt == 8)
  {
    while (this.jm221.size() > 0)
      try
      {
        DBConn localDBConn = (DBConn)this.jm221.remove(0);
        if (localDBConn != null)
          localDBConn.release();
      }
      catch (Exception localException2)
      {
        jm9.error(localException2);
      }
  }
  else if (paramInt == 16)
  {
    paramDBConn = null;
    if (this.jm221.size() > 0)
      paramDBConn = (DBConn)this.jm221.remove(0);
    else
      paramOutParam.jm18 = true;
    if (paramDBConn != null)
    {
      this.jm220 += 1;
      this.jm54.counterAdd();
    }
    return paramDBConn;
  }
  return null;
}

public int getDBType()
{
  return this.jm53.getDBType();
}

public int getPoolSize()
{
  return this.jm53.getMaxConnections();
}

public int getUsableSize()
{
  return (this.jm53.getMaxConnections() - this.jm220);
}

public int getUseSize()
{
  return this.jm220;
}

public int getIdleSize()
{
  return this.jm221.size();
}

public String getId()
{
  return this.jm53.getId();
}

public boolean freeAllConn()
{
  try
  {
    jm219(8, null, null);
  }
  catch (DatabaseException localDatabaseException)
  {
    jm9.error(localDatabaseException);
  }
  return true;
}

public int getTranLevel(boolean paramBoolean)
{
  return this.jm53.getTranLevel(paramBoolean);
}

public void setConfig(DBConfig paramDBConfig)
{
  this.jm53 = paramDBConfig;
}

public void setDriver(Driver paramDriver)
{
  this.jm230 = paramDriver;
}

protected class OutParam
{
  boolean jm18 = false;
}
}